export default {
    name: 'LoginView',
    data() {
        return {
            form: {
                name: '',
                pwd: ''
            }
        }
    },
    methods: {
        login() {
            if (this.form.name.length < 1 || this.form.pwd.length < 1) {
                this.$message({
                    type: 'error',
                    message: '请输入用户名、密码!',
                    duration: 1000
                });
                return
            }
            this.axios.post('http://localhost:3000/api/login', { userName: this.form.name, userPwd: this.form.pwd })
                .then(res => {
                    if (res.data.message == 'ok') {
                        localStorage.setItem('token', res.data.user)
                        //保存当前登录用户完整信息
                        localStorage.setItem('currentUser', JSON.stringify(res.data.data))
                        localStorage.setItem('userLike', 6)
                        location.href = 'http://localhost:8080/index'
                        this.$router.push('/index')
                    } else {
                        this.$message({
                            type: 'error',
                            message: '请检查用户名、密码是否正确!',
                            duration: 1000
                        });
                        this.form.name = ''
                        this.form.pwd = ''
                    }
                }
                )
        },
        goindex() {
            this.$router.push('/index')
        },
        goregister() {
            this.$router.push('/register')
        },
    }
}